import {
	pathToBase64
} from './base64.js'

export const ImageToBase64 = (file) => {
	return new Promise((resolve, reject) => {
		uni.getImageInfo({
			src: file.path,
			success: res => {
				const imageInfo = res
				// #ifdef WEB
				const reader = new FileReader();
				reader.onload = function() {
					resolve({
						...imageInfo,
						base64: reader.result,
						type: file.type.split('/')[1]
					});
					console.log(imageInfo);
				};
				reader.onerror = function(error) {
					reject(error);
				};
				reader.readAsDataURL(file);
				// #endif
				// #ifdef APP
				uni.saveFile({
					tempFilePath: file.path,
					success: saveFile => {
						pathToBase64(saveFile.savedFilePath).then(base64 => {
							resolve({
								...imageInfo,
								base64
							});
						})
						console.log(imageInfo);
					}
				})
				// #endif
			}
		})
	});
}